package edu.ccut.saturn.workflow.finishTaskModule.code;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.WorkFlowTools;


public class UpdateWorkFlowInstanceContent extends DefaultComponent {
	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		//WorkFlowInfo workFlowInfo = (WorkFlowInfo)parameterContext.getParameterValue(0);
		HashMap<String,String> customParamMap = (HashMap<String,String>)parameterContext.getParameterValue(1);
//		
    	SaturnData workFlowInstance = (SaturnData)parameterContext.getParameterValue(2);
		Connection conn = parameterContext.getConnection();
//		JexlContext context = JexlHelper.createContext();
//		Map<String, String> values = context.getVars();
//		// 获得流程实例上下文数据流
//		InputStream dataStream = getDataInputStream(conn, currentActivityInstance.get("process_instance_id")+"");
//		List<String> keyList = getKeyList(dataStream,values);
//		// 获得当前工作项对应活动的定义信息
//		ActivityInfo activity = workFlowInfo.getActivityInfoById(currentActivityInstance.get("activity_id")+"");
//
//		List<DataInfo> dataInfos = activity.getFormData();
//		for (int i = 0; i < dataInfos.size(); i++) {
//			DataInfo dataInfo = dataInfos.get(i);
//			String key = dataInfo.getKey();
//			String value = (String) customParamMap.get(key);
//			if (value != null && !value.equals("")) //$NON-NLS-1$
//				values.put(key, value);
//			if (keyList.indexOf(dataInfo.getKey()) == -1)
//				keyList.add(dataInfo.getKey());
//		}
//		byte[] newContent = getXmlByDataMap(keyList, values);
		byte[] newContent = getXmlCustomParamMap(customParamMap);
		SaturnData workFlowInstanceForUpdate= WorkFlowTools.getProcessInstance();
		workFlowInstanceForUpdate.set("content", newContent);
		workFlowInstanceForUpdate.set("process_instance_id", workFlowInstance.get("process_instance_id"));
		//更新content
		Operation.dataBase().updateByPrimaryKey(workFlowInstanceForUpdate, "", conn);
	}
//	/**
//	 * 根据map获得xml
//	 * 
//	 * @param dataMap
//	 * @return
//	 * @throws UnsupportedEncodingException
//	 */
//	@SuppressWarnings("unchecked")//$NON-NLS-1$
//	private byte[] getXmlByDataMap(List<String> keyList, Map dataMap)
//			throws UnsupportedEncodingException {
//		StringBuffer sb = new StringBuffer();
//		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
//		sb.append("<root>"); //$NON-NLS-1$
//		sb.append("<data>"); //$NON-NLS-1$
//		for (int i = 0; i < keyList.size(); i++) {
//			String name = keyList.get(i);
//			sb.append("<" + name + ">"); //$NON-NLS-1$ //$NON-NLS-2$
//			sb.append(dataMap.get(name));
//			sb.append("</" + name + ">"); //$NON-NLS-1$ //$NON-NLS-2$
//		}
//		sb.append("</data>"); //$NON-NLS-1$
//		sb.append("</root>"); //$NON-NLS-1$
//		return sb.toString().getBytes("UTF-8"); //$NON-NLS-1$
//	}
	@SuppressWarnings("unchecked")//$NON-NLS-1$
	private byte[] getXmlCustomParamMap(Map<String,String> dataMap)
			throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
		sb.append("<root>"); //$NON-NLS-1$
		sb.append("<data>"); //$NON-NLS-1$
		for(Entry data:dataMap.entrySet()){
			sb.append("<" + data.getKey() + ">");
			sb.append(data.getValue());
			sb.append("</" + data.getKey() + ">");
		}
		sb.append("</data>"); //$NON-NLS-1$
		sb.append("</root>"); //$NON-NLS-1$
		return sb.toString().getBytes("UTF-8"); //$NON-NLS-1$
	}
//	/**
//	 * 得到流程数据xml流
//	 * 
//	 * @param conn
//	 * @return
//	 * @throws SQLException
//	 */
//	private InputStream getDataInputStream(Connection conn,
//			String workFlowInstanceId) throws SQLException {
//		InputStream inputStream = null;
//		Statement stm = null;
//		ResultSet rs = null;
//		try {
//			stm = conn.createStatement();
//			String sql = "select CONTENT from WF_PROCESS_INSTANCE where PROCESS_INSTANCE_ID=" //$NON-NLS-1$
//					+ workFlowInstanceId;
//			rs = stm.executeQuery(sql);
//			if (rs.next()) {
//				Blob blob = rs.getBlob(1);
//				if (blob != null) {
//					inputStream = blob.getBinaryStream();
//				}
//			}
//		} finally {
//			rs.close();
//			stm.close();
//		}
//		return inputStream;
//	}
//	/**
//	 * 得到数据map
//	 * 
//	 * @param inputStream
//	 * @return
//	 * @throws SaturnComponentException
//	 */
//	@SuppressWarnings("unchecked")//$NON-NLS-1$
//	private List<String> getKeyList(InputStream inputStream,Map<String,String> values)
//			throws SaturnComponentException {
//		List<String> list = new ArrayList<String>();
//		if (inputStream != null) {
//			Document document = Dom4jUtils.getDocment(inputStream);
//			Element root = document.getRootElement();
//			Element dataElement = root.element("data"); //$NON-NLS-1$
//			List elementList = dataElement.elements();
//			for (int i = 0; i < elementList.size(); i++) {
//				Element element = (Element) elementList.get(i);
//				String key = element.getName();
//				String value = element.getText();
//				values.put(key, value);
//				if (list.indexOf(key) == -1)
//					list.add(key);
//			}
//		}
//		return list;
//	}

}